﻿/*  Copyright © 2012 Matthew David Elgert - mdelgert@yahoo.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{

    public static class Log
    {

        public static void Msg(string msg, bool isError = false)
        {

            if (Common.GetLogFile() != null)
            {


                // Create a writer and open the file
                StreamWriter log;
                string Message = (DateTime.Now + " : " + msg);

                //Write msg to console
                Console.WriteLine(Message);

                if (!File.Exists(Common.GetLogFile()))
                {
                    log = new StreamWriter(Common.GetLogFile());
                }
                else
                {
                    log = File.AppendText(Common.GetLogFile());
                }

                //Write log to DB
                if (Common.GetLogDB() != null)
                {
                    string LogMsg = WriteLog(msg, isError);

                    if (LogMsg != null)
                    {
                        log.WriteLine(LogMsg);
                        Console.WriteLine(LogMsg);
                    }

                }

                // Write to the file
                log.WriteLine(Message);
                log.WriteLine();

                // Close the stream
                log.Close();
            }
            else
            {
                Console.WriteLine("Access to log file denied or no log file...");
            }
        }

        public static string WriteLog(string msg, bool isError)
        {
            string Response = null;

            //Create DB
            try
            {
                StreamReader streamReader = new StreamReader("DBCreate.sql");
                string SQL = streamReader.ReadToEnd();
                SQL = SQL.Replace("LogDBName", Common.GetLogDB());
                using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString()))
                {
                    using (SqlCommand cmd = new SqlCommand(SQL, cnn))
                    {

                        cnn.Open();
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                        cnn.Close();
                    }
                }

            }
            catch (System.Exception ex)
            {
                Response = ex.ToString();
            }

            //Create Table
            try
            {
                StreamReader streamReader = new StreamReader("DBTable.sql");
                string SQL = streamReader.ReadToEnd();
                SQL = SQL.Replace("LogDBName", Common.GetLogDB());
                using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString()))
                {
                    using (SqlCommand cmd = new SqlCommand(SQL, cnn))
                    {
                        cnn.Open();
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                        cnn.Close();
                    }
                }
            }
            catch (System.Exception ex)
            {
                Response = ex.ToString();
            }


            //Write to DB
            try
            {
                using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString()))
                {
                    string SQL = @"INSERT INTO [" + Common.GetLogDB() + @"].[dbo].[Log] ([Msg], [Error]) VALUES (@txtMsg, @error)";
                    using (SqlCommand cmd = new SqlCommand(SQL, cnn))
                    {
                        cnn.Open();
                        cmd.Parameters.Add("@txtMsg", SqlDbType.Text);
                        cmd.Parameters["@txtMsg"].Value = msg;
                        cmd.Parameters.Add("@error", SqlDbType.Bit);
                        cmd.Parameters["@error"].Value = isError;
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                        cnn.Close();
                    }
                }
            }
            catch (System.Exception ex)
            {
                Response = ex.ToString();
            }

            return Response;
        }

    }
}
